package com.itboye.sunsun.person.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.sunsun.custome.NoPreViewPager;
import com.itboye.sunsun.message.ui.MessageActivity;
import com.itboye.sunsun.personsetting.ui.OrderViewPagerAdapter;
import com.itboye.sunsun.product.center.KeFuFragment;
import com.itboye.sunsun.product.center.ProducenterProducenterFragemnt;
import com.itboye.sunsun.product.center.ProducenterShiPinFragment;
import com.itboye.sunsunhome.R;

public class MyOrderActivity extends FragmentActivity implements
		OnClickListener {

	private NoPreViewPager mPager;
	private ArrayList<Fragment> fragmentList;
	private TextView btnTiezi, btnTieziHuifu, btnTiezikefu;
	private ImageView image;
	private int currIndex;// 当前页卡编号
	private float offset;// 图片移动的偏移量
	private RelativeLayout backrela;
	private ImageView message;
	private ImageView imgdetails, imgvideo, imgartificial;
	private LinearLayout layoutMycollTiezi, layoutTieZiVideo, layoutTieZiHkefu,
			linOne, linTwo, linThree;
	private TextView add_shap_title_tv;
	private ImageView close_icon;
	private String title;

	@Override
	protected void onCreate(Bundle savedInstance) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_myorder);
		
		
		title = getIntent().getStringExtra("title");
		add_shap_title_tv = (TextView) findViewById(R.id.add_shap_title_tv);
		btnTiezi = (TextView) findViewById(R.id.btnTiezi);
		btnTieziHuifu = (TextView) findViewById(R.id.btnTieziHuifu);
		btnTiezikefu = (TextView) findViewById(R.id.btnTiezikefu);
		imgdetails = (ImageView) findViewById(R.id.imgdetails);
		imgvideo = (ImageView) findViewById(R.id.imgvideo);
		imgartificial = (ImageView) findViewById(R.id.imgartificial);
		add_shap_title_tv.setText(title);
		close_icon = (ImageView) findViewById(R.id.close_icon);
		close_icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		initTextView();
		initImage();
		initViewPager();
		// setStatusBarColor(R.color.home_blue);
	}

	// 初始化标签
	public void initTextView() {
		layoutMycollTiezi = (LinearLayout) findViewById(R.id.layoutMycollTiezi);
		layoutTieZiVideo = (LinearLayout) findViewById(R.id.layoutTieZiVideo);
		layoutTieZiHkefu = (LinearLayout) findViewById(R.id.layoutTieZiHkefu);

		layoutMycollTiezi.setOnClickListener(new txListener(0));
		layoutTieZiVideo.setOnClickListener(new txListener(1));
		layoutTieZiHkefu.setOnClickListener(new txListener(2));

	}

	public class txListener implements OnClickListener {
		private int index = 0;

		public txListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mPager.setCurrentItem(index);
		}
	}

	/*
	 * 初始化图片的位移像素
	 */
	public void initImage() {
		image = (ImageView) findViewById(R.id.cursor);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		int screenH = dm.heightPixels;
		offset = screenW / 3.0f;

		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				(int) offset - 30, 2);
		image.setLayoutParams(params);

		Log.v("Hello-----screeW", "" + screenW);
		Log.v("Hello-----bmpW", "" + "" + screenH);

	}

	/*
	 * 初始化ViewPager
	 */
	public void initViewPager() {
		mPager = (NoPreViewPager) findViewById(R.id.viewpager);
		fragmentList = new ArrayList<Fragment>();
		Fragment allFragment = new ProducenterProducenterFragemnt();
		Fragment payFragment = new ProducenterShiPinFragment();
		Fragment sendFragment = new KeFuFragment();

		fragmentList.add(allFragment);
		fragmentList.add(payFragment);
		fragmentList.add(sendFragment);

		Intent intent = getIntent();
		int i = intent.getIntExtra("fragmentCurrent", 0);
		currIndex = i;
		//
		// //给ViewPager设置适配器
		mPager.setAdapter(new OrderViewPagerAdapter(
				getSupportFragmentManager(), fragmentList));

		mPager.setCurrentItem(i);// 设置当前显示标签页为第一页
		setTextColor(i + 1);

		tranSlate(i, 0);

		// Animation animation = new TranslateAnimation(currIndex * one, i
		// * one, 0, 0);// 平移动画
		// currIndex = i;
		// animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
		// animation.setDuration(0);// 动画持续时间0.2秒
		// image.startAnimation(animation);// 是用ImageView来显示动画的

		mPager.setOnPageChangeListener(new MyOnPageChangeListener());// 页面变化时的监听器

	}

	public class MyOnPageChangeListener implements com.itboye.sunsun.custome.NoPreViewPager.OnPageChangeListener {
		private float one = offset;// 两个相邻页面的偏移量

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub

			// Toast.makeText(App.ctx, ""+arg0, 0).show();
			// Animation animation = new TranslateAnimation(currIndex * one,
			// arg0
			// * one, 0, 0);// 平移动画
			// currIndex = arg0;
			// animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
			// animation.setDuration(200);// 动画持续时间0.2秒
			// image.startAnimation(animation);// 是用ImageView来显示动画的

			tranSlate(arg0, 200);
			currIndex = arg0;
			int i = currIndex + 1;
			setTextColor(i);

			// Toast.makeText(MyOrderActivity.this, "您选择了第"+i+"个页卡",
			// Toast.LENGTH_SHORT).show();
		}
	}

	// 平移动画
	public void tranSlate(int currentItem, long durationMillis) {
		float one = offset;
		Log.v("offest", "" + offset);
		Animation animation = new TranslateAnimation(currIndex * one,
				currentItem * one, 0, 0);// 平移动画
		Log.v("currentoffest", "" + currIndex * one);
		Log.v("currentItem", "" + currentItem * one);
		animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
		animation.setDuration(durationMillis);// 动画持续时间0.2秒
		image.startAnimation(animation);// 是用ImageView来显示动画的
	}

	// 设置顶部条目颜色的变化
	public void setTextColor(int i) {
		switch (i) {
		case 1:
			btnTiezi.setTextColor(getResources().getColor(R.color.home_blue));
			btnTieziHuifu.setTextColor(getResources().getColor(R.color.gray));
			btnTiezikefu.setTextColor(getResources().getColor(R.color.gray));
			imgdetails.setBackgroundResource(R.drawable.produceter_details);
			imgvideo.setBackgroundResource(R.drawable.video);
			imgartificial.setBackgroundResource(R.drawable.artificial);
			break;
		case 2:
			btnTiezi.setTextColor(getResources().getColor(R.color.gray));
			btnTieziHuifu.setTextColor(getResources().getColor(
					R.color.home_blue));
			btnTiezikefu.setTextColor(getResources().getColor(R.color.gray));
			imgdetails.setBackgroundResource(R.drawable.imgchanpin);
			imgvideo.setBackgroundResource(R.drawable.shipin);
			imgartificial.setBackgroundResource(R.drawable.artificial);
			break;
		case 3:
			btnTiezi.setTextColor(getResources().getColor(R.color.gray));
			btnTieziHuifu.setTextColor(getResources().getColor(R.color.gray));
			btnTiezikefu.setTextColor(getResources()
					.getColor(R.color.home_blue));
			imgdetails.setBackgroundResource(R.drawable.imgchanpin);
			imgvideo.setBackgroundResource(R.drawable.video);
			imgartificial.setBackgroundResource(R.drawable.imgkefu);
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.backrela:
			this.finish();
			break;
		case R.id.message:
			Intent intent = new Intent(this, MessageActivity.class);
			intent.putExtra("type", "shop");
			startActivity(intent);

		default:
			break;
		}

	}


}
